package joc.internal.transformer.helper;

import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.List;

import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtMethod;
import javassist.NotFoundException;

public class InheritedImplicitContract extends ImplicitContract {
	private Class<?> affectedClass;
	private String affectedMethodName;

	public InheritedImplicitContract(String configValue, Class<? extends Annotation> contractAnnotation,
			Class<?> affectedClass, String affectedMethodName) {
		super(configValue, contractAnnotation);
		this.affectedClass = affectedClass;
		this.affectedMethodName = affectedMethodName;
	}

	@Override
	public List<CtMethod> getAffectedMethods(ClassPool pool, CtClass targetClass) throws NotFoundException {
		List<CtMethod> affectedMethods = new ArrayList<CtMethod>();
		CtClass affectedClassResolved = pool.get(affectedClass.getName());
		CtMethod affectedMethod = affectedClassResolved.getDeclaredMethod(affectedMethodName);
		for (CtMethod targetMethod : targetClass.getDeclaredMethods()) {
			if (targetMethod.equals(affectedMethod)) {
				affectedMethods.add(targetMethod);
			}
		}
		return affectedMethods;
	}

}
